﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using TotalCrytography;
using TotalCrytography.Data;
using System.Windows.Forms;
using System.IO;

namespace WordAddInCrytography
{
    public partial class CrytographyPlugin : OfficeRibbon
    {
        private bool isLogin;
        private UserDTO m_loginUser;
        private string m_loginPass;
        public CrytographyPlugin()
        {
            InitializeComponent();
            Init(false);
        }

        private void CrytographyPlugin_Load(object sender, RibbonUIEventArgs e)
        {
            DataProvider.ConnectionString = MainGUI.GetConfig();
        }

        private void Login_Click(object sender, RibbonControlEventArgs e)
        {
            Login();
        }

        private void Init(bool state)
        {
            rbLogin.Enabled = !state;
            rbLogout.Enabled = state;
            rbSign.Enabled = state;
            if (state)
            {
                isLogin = true;
            }
            else
            {
                isLogin = false;
                m_loginUser = null;
                m_loginPass = null;
            }
        }

        private void Login()
        {
            LoginGUI loginFrm = new LoginGUI();
            if (loginFrm.ShowDialog() == DialogResult.OK)
            {
                m_loginUser = loginFrm.LoginUser;
                m_loginPass = loginFrm.LoginPassword;
                Init(true);
            }
        }

        private void Logout()
        {
            if (MessageBox.Show("Do you really want to log out ?", "Log out", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Init(false);
            }
        }

        private void rbLogout_Click(object sender, RibbonControlEventArgs e)
        {
            Logout();
        }

        private void rbEncrypt_Click(object sender, RibbonControlEventArgs e)
        {
            if (SaveFile())
            {
                string filename = Globals.ThisAddIn.Application.ActiveDocument.FullName;
                List<string> lst = new List<string>();
                lst.Add(filename);
                EncryptGUI encryptFrm = new EncryptGUI(lst);
                encryptFrm.Show();
            }
        }

        private void rbDecrypt_Click(object sender, RibbonControlEventArgs e)
        {
            if (SaveFile())
            {
                string filename = Globals.ThisAddIn.Application.ActiveDocument.FullName;
                List<string> lst = new List<string>();
                lst.Add(filename);
                DecryptGUI decryptFrm = new DecryptGUI(lst,m_loginPass,m_loginUser.PrivateKey);
                decryptFrm.Show();
            }
        }

        private void rbSign_Click(object sender, RibbonControlEventArgs e)
        {
            if (SaveFile())
            {
                string filename = Globals.ThisAddIn.Application.ActiveDocument.FullName;
                List<string> lst = new List<string>();
                lst.Add(filename);
                SignGUI signFrm = new SignGUI(lst);
                signFrm.Show();
            }
        }

        private void rbVerify_Click(object sender, RibbonControlEventArgs e)
        {
            if (SaveFile())
            {
                string filename = Globals.ThisAddIn.Application.ActiveDocument.FullName;
                List<string> lst = new List<string>();
                lst.Add(filename);
                CheckGUI checkFrm = new CheckGUI(lst);
                checkFrm.Show();
            }
        }

        private bool IsSavedFile()
        {
            return Globals.ThisAddIn.Application.ActiveDocument.Saved;
        }

        private bool SaveFile()
        {
            if (!IsSavedFile())
                if (MessageBox.Show("You must save this document!\nDo you want to save this file?", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        Globals.ThisAddIn.Application.ActiveDocument.Save();
                    }
                    catch (System.Exception ex)
                    {
                    	
                    }
                }
            return IsSavedFile();
        }

        private void rbRegister_Click(object sender, RibbonControlEventArgs e)
        {
            RegisterAccountGUI registerFrm = new RegisterAccountGUI();
            if (registerFrm.ShowDialog() == DialogResult.OK)
            {
                Init(true);
                if (!isLogin)
                {
                    m_loginUser = registerFrm.User;
                    m_loginPass = registerFrm.Password;
                }
            }
        }
    }
}
